package com.miplus.ccrm.report.security;

import com.miplus.ccrm.common.JDataService;
import com.miplus.ccrm.common.JPubUtils;
import com.miplus.ccrm.common.JSysUser;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Objects;

@Component
public class JCommonData {
    @Resource
    private JDataService dataService;

    public String getCurrUsrID()
    {
        HttpServletRequest h=((ServletRequestAttributes) Objects.requireNonNull(RequestContextHolder.getRequestAttributes())).getRequest();
        String token= JPubUtils.getToken(h);
        String key = JPubUtils.getTokenKey(token);
        JSysUser user = (JSysUser) dataService.get(key);
        return user.getUsrid();
    }

    public JSysUser getCurrUsr()
    {
        HttpServletRequest h=((ServletRequestAttributes) Objects.requireNonNull(RequestContextHolder.getRequestAttributes())).getRequest();
        String token= JPubUtils.getToken(h);
        String key = JPubUtils.getTokenKey(token);
        return (JSysUser) dataService.get(key);
    }

    public void SetUsrSmsVCode(String usrid,String code)
    {
        String keyid=JPubUtils.getVCodeIden(usrid);
        dataService.set(keyid,code,JPubUtils.vcodeExpiration);
    }
}
